package com.yj.consumer.annotation;

import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

@Slf4j
public class CheckLengthValidator implements ConstraintValidator<CheckLength,Object> {

    private int min;
    private int max;

    @Override
    public void initialize(CheckLength constraintAnnotation) {
        min = constraintAnnotation.min();
        max = constraintAnnotation.max();
        log.info("初始化方法执行。。。,{}",System.currentTimeMillis());
        System.out.println("initialize...");
    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {

        if (o == null){
            return true;
        }
        if (o.toString().trim().length()>2&&o.toString().trim().length()<10){
            return true;
        }
        return false;
    }
}
